package com.xue.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xue.bean.Product;
import com.xue.bean.SeckillMessage;
import com.xue.mq.MQSender;
import com.xue.service.IDubboService;

@RestController
public class ConsumerController {
	
	@Reference
	IDubboService dubboService;
	
	@Autowired
	MQSender mq;
	
	private int num = 0;
	
	//private ExecutorService cachedThreadPool = Executors.newFixedThreadPool(10);
	
	@RequestMapping("/getShop")
	public String getShop(Product pro, String userId) {
		System.out.println("*********dubbo 开始*********************");
		SeckillMessage message = new SeckillMessage();
		message.setProductId(pro.getProductId());
		message.setUserId(userId);
		num ++;
		System.err.println("总数:"+num);
		mq.sendSeckillMessage(message);
		//Object obj = mq.sendAndReceive("", message);
		//System.err.println("***:"+obj);
		//String rtnStr = dubboService.getShop(pro, userId);
		//System.out.println("*********dubbo 结束*********************");
		return "等待中";
	}
	@RequestMapping("/buy")
	public Object buy(Product pro, String userId) {
		System.out.println("*********线程购买 开始*********************");
		SeckillMessage message = new SeckillMessage();
		message.setProductId(pro.getProductId());
		message.setUserId(userId);
		num ++;
		System.err.println("总数:"+num);
		//mq.sendSeckillMessage(message);
		//Object obj = mq.sendAndReceive("", message);
		//System.err.println("***:"+obj);
		//String rtnStr = dubboService.getShop(pro, userId);
		//System.out.println("*********dubbo 结束*********************");
//		Runnable r =new Runnable() {
//            @Override
//            public void run() {
//            	System.out.println("***********进入线程执行*************");
//            	dubboService.getShop(pro, userId);
//            }
//        };
//        new Thread(r).start();	
        
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        Object rtnObj = null;
        Future result1 = cachedThreadPool.submit( new Callable() {
            @Override
            public String call() throws Exception {
                //int sum = 0;
                //Thread.sleep( 1000 );
                //System.out.println(sum+"22222222222222"+Thread.currentThread().getName());
                String rstr = dubboService.getShop(pro, userId);
                System.err.println("结果:"+rstr);
            	return rstr;
            }

        });
        try {
        	 //m.put("rtn",result1.get());
        	rtnObj = result1.get();
        	
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
//      	cachedThreadPool.execute(new Runnable(){
//			@Override
//			public void run() {
//				System.out.println("***********进入线程执行*************");
//            	dubboService.getShop(pro, userId);
//			
//			}	        	   
//         });  //创建 n 个线程。
//		      
		 cachedThreadPool.shutdown(); 
		 	 
		return rtnObj;
	}
	@RequestMapping("/getUserShop")
	public String getUserShop(Product pro , String userId) {
		dubboService.getUserShop(pro, userId);
		return "成功";
	}
	
}
